package com.ghck.shop.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.ghck.shop.constant.FlowType;
import com.ghck.shop.dao.BankMapper;
import com.ghck.shop.dao.FlowWaterMapper;
import com.ghck.shop.dao.TiXianMapper;
import com.ghck.shop.model.Bank;
import com.ghck.shop.model.FlowWater;
import com.ghck.shop.model.TiXian;
import com.ghck.shop.service.OperateService;
import com.ghck.shop.utils.FileStoreUtil;

@Service
public class OperateServiceImpl implements OperateService{

	@Autowired
	private BankMapper bankMapper;
	@Autowired
	private TiXianMapper tiXianMapper;
	@Autowired
	private FlowWaterMapper flowWaterMapper;
	
	@Override
	public List<Bank> allBanks() {
		List<Bank> list = bankMapper.allBanks();
		if(!CollectionUtils.isEmpty(list)){
			for(Bank bank:list){
				if(StringUtils.isNotEmpty(bank.getPic())){
					bank.setPic(FileStoreUtil.getBaseUrl().concat(bank.getPic()));
				}
			}
		}
		return list;
	}

	@Override
	public Integer tixian(TiXian tiXian) {
		return tiXianMapper.saveTiXian(tiXian);
	}

	@Override
	public List<FlowWater> listMyFlowWaters(FlowWater flowWater) {
		List<FlowWater> list = flowWaterMapper.listFlowWaters(flowWater);
		if(!CollectionUtils.isEmpty(list)){
			for(FlowWater fw:list){
				if(StringUtils.isNotEmpty(fw.getPic())){
					fw.setPic(FileStoreUtil.getBaseUrl().concat(fw.getPic()));
				}
			}
		}
		return list;
	}

	@Override
	@Transactional
	public void tixianComplete(Integer id) {
		TiXian tixian = tiXianMapper.findTiXianById(id);
		if(tixian != null){
			flowWaterMapper.myMoney(tixian.getUserId());
			tiXianMapper.updateTiXianComplete(id);
			Bank bank = bankMapper.findBankById(tixian.getBankId());
			FlowWater fw = new FlowWater();
			fw.setUserId(tixian.getUserId());
			fw.setType(FlowType.WITHDRAW_DEPOSIT);//提现
			fw.setPic(bank.getPic());
			fw.setDescribe("提现");
			fw.setAmount(-tixian.getAmount());
			flowWaterMapper.saveFlowWater(fw);
		}
	}

	@Override
	public List<TiXian> adminTixianlist(TiXian tiXian, Integer start,Integer limit) {
		return tiXianMapper.adminTixianlist(tiXian, start, limit);
	}

	@Override
	public Integer countAdminTixianlist(TiXian tiXian) {
		return tiXianMapper.countAdminTixianlist(tiXian);
	}

	

}
